package cn.edu.nju.newsFeed;

import java.io.IOException;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class EmotionAdapter extends BaseAdapter{
	private Activity context;
	private String[] emotions;
	
	public EmotionAdapter(Activity activity){
		this.context = activity;
		try {
			emotions = activity.getAssets().list("emotion");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getCount(){
		return emotions.length;
	}
	
	public String getItem(int position){
		return emotions[position];
	}
	
	public long getItemId(int position){
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		ImageView imageView;
		if(convertView == null){
			imageView = new ImageView(context);			
		}
		else{
			imageView = (ImageView)convertView;
		}
		
		try {
			Drawable emotionDrawable = Drawable.createFromStream(context.getAssets().open("emotion/" + emotions[position]), null);
			imageView.setBackgroundDrawable(emotionDrawable);
			imageView.setLayoutParams(new GridView.LayoutParams(33, 33));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return imageView;
	}
}
